package com.hotec.PROJECT_NAME.modules.sys.service;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hotec.PROJECT_NAME.component.basic.BaseService;
import com.hotec.PROJECT_NAME.component.basic.ConstantKey;
import com.hotec.PROJECT_NAME.modules.sys.entity.SysAttachment;
import com.hotec.PROJECT_NAME.modules.sys.mapper.SysAttachmentMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

;

/**
 * @author llb
 * @date
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysAttachmentService extends BaseService<SysAttachment, SysAttachmentMapper> {


    /**
     * 批量保存附件
     * @param attachmentList
     * @param businessId
     */
    public void saveAttachments(List attachmentList, String businessId) {
        if (CollectionUtils.isEmpty(attachmentList)) {
            return;
        }
        //全量删除
        deleteAttachmentByBizId(businessId);

        //重新保存
        for (Object item : attachmentList) {
            SysAttachment sysAttachment;
            if(item instanceof  LinkedHashMap){
                 sysAttachment = BeanUtil.toBean(item, SysAttachment.class);
            }else{
                sysAttachment= (SysAttachment) item;
            }
            sysAttachment.setId(null);
            sysAttachment.setBusinessId(businessId);
            this.save(sysAttachment);
        }
    }

    /**
     * 根据附件Id查询附件列表
     * @param businessIds
     * @return
     */
    public Map<String, List<SysAttachment>> queryAttachmentListByBusinessIds(List<String> businessIds) {
        Example example = new Example(SysAttachment.class);
        example.createCriteria().andIn("businessId", businessIds);
        List<SysAttachment> sysAttachments = this.queryByExample(ConstantKey.DEFAULT_ORDER_BY, example);
        //业务id和版本合并后作为key，附件列表作为value
        Map<String, List<SysAttachment>> resultMap = Maps.newHashMap();
        if (CollectionUtils.isEmpty(sysAttachments)) {
            return Maps.newHashMap();
        }
        for (SysAttachment attachment : sysAttachments) {
            String key = attachment.getBusinessId();
            if (StringUtils.isEmpty(key)) {
                continue;
            }
            if (!resultMap.containsKey(key)) {
                resultMap.put(key, Lists.newArrayList());
            }
            resultMap.get(key).add(attachment);
        }
        return resultMap;
    }

    public void deleteAttachmentByBizId(String businessId) {
        Example example = new Example(SysAttachment.class);
        example.createCriteria().andEqualTo("businessId", businessId);
        this.mapper.deleteByExample(example);
    }

    public void remove(List<SysAttachment> sysAttachmentList) {
        if (CollectionUtils.isNotEmpty(sysAttachmentList)) {
            super.deleteById(sysAttachmentList);
        }
    }
}
